/**
 * This class is the main view for the application. It is specified in app.js as
 * the "autoCreateViewport" property. That setting automatically applies the
 * "viewport" plugin to promote that instance of this class to the body element.
 * 
 * TODO - Replace this content of this view to suite the needs of your
 * application.
 */

// 主页
Ext.define('app.view.main.Main', {
	extend : 'Ext.container.Container',

	xtype : 'app-main',
	uses : ['app.view.main.region.Top', 'app.view.main.region.Left',
			'app.view.main.region.Foot'],

	layout : {
		type : 'border'
	},
	items : [{
		region : 'north',
		id : '__nortPanel',
		xtyp : 'main-top',

		contentEl : 'app-header',
		height : 108
	}, {
		region : 'west',
		width : 220,
		xtype : 'main-left',
		id : 'west-panel'
	}, {
		region : 'center',
		id : 'centerTabPanel',
		xtype : 'tabpanel',
		deferredRender : true,
		enableTabScroll : true,
		activeTab : 0,
		autoDestroy : true,
		plugins : Ext.create('Ext.ux.TabCloseMenu'),
		defaults : {
			closeAction : 'destroy',
			closable : true
		},
		items : []
	}, {
		region : 'south',
		xtype : 'main-foot',
		height : 40
	}],
	// 构造函数
	constructor : function() {
		var me = this;
		// 加上首页的导航菜单
		App.init();
		// 初始化顶部菜单
		this.callParent(arguments);
		this.afterPropertySet();
	},

	// 初始化后
	afterPropertySet : function() {
		// 统计按钮
		var addBtn = function(onlineCnt, totalCnt, todayCnt) {
			var megBtn = Ext.getCmp('messageTip');
			megBtn
					.setHtml('<div style="height:25px;">当前在线人数<strong style="color: red;">'
							+ onlineCnt
							+ '</strong>，'
							+ '今日访问量<strong style="color: red;">'
							+ todayCnt
							+ '</strong>，'
							+ '总访问量<strong style="color: red;">'
							+ totalCnt + '</strong></div>');
		};
		// 获得在线人数
		var addBtnFunction = function() {
			Ext.Ajax.request({
						url : __ctxPath + '/comm/countIndexLog.do',
						method : 'POST',
						success : function(response, options) {
							var result = Ext.util.JSON
									.decode(response.responseText);
							var onlineCnt = result.onlineCnt;
							var totalCnt = result.totalCnt;
							var todayCnt = result.todayCnt;
							addBtn(onlineCnt, totalCnt, todayCnt);
							// 设2分钟更新一次
							setTimeout(addBtnFunction, 900 * 60);
						},
						failure : function(response, options) {
						},
						scope : this
					});
		};

		var me = this;
		var showflag = false;
		// 使用jquery
		for (var i = 0; i < curUserInfo.topModules.length; i++) {
			var mod = curUserInfo.topModules[i];
			if (mod == null)
				continue;
			$('#top-nav').append("<li><a href='javascript:void(0)' id='"
					+ mod.id + "' name='" + mod.id + "'>" + "<div><i class='"
					+ mod.iconCls + " fa-2x'></i></div> <span>" + mod.title
					+ "</span></a></li>");
		}
		$('#top-nav li a').each(function(e) {
			$(this).click(function() {
						$("#top-nav li.active").removeClass("active");
						$(this).parent().addClass("active");
						var expires = new Date();
						expires.setDate(expires.getDate() + 300);
						setCookie("_topNavId", $(this)[0].id, expires,
								__ctxPath);
						// 切换左菜单
						me.loadWestMenu(showflag);
						showflag = true;
					});
		});

		setTimeout(function() {
					var actTabId = getCookie('_topNavId');
					// 激活上一次点击的Panel
					if (actTabId && $('#' + actTabId).size() > 0) {
						$('#' + actTabId).click();
					} else { // 激活第一个
						$('#' + curUserInfo.topModules[0].id).click();
					}
					// 打开个人桌面
					App.clickTopTab('AppHome');
					// 统计人数
					addBtnFunction();
				}, 300);
	},
	// 加菜中间菜单
	showMenu : function(arr, topMenuId) {
		var menus = arr[0].subXml.Menus.children;
		// 点击“首页”
		if (topMenuId == "desktop") {
			App.clickTopTab('AppHome', {}, function() {
						if (Ext.getCmp('MenuDataView')) {
							Ext.getCmp('MenuDataView').store.loadData(menus);
						}
					});
			return;
		}
		// 点击其它
		App.clickTopTab('MenuHome', {
					menus : menus
				}, null, function() {
					Ext.getCmp('MenuDataView').store.loadData(menus);
				}, true);
	},
	// 加载左侧菜单
	loadWestMenu : function(show) {
		var me = this;
		var westPanel = Ext.getCmp('west-panel');
		var iconCls = $("#top-nav li.active").find('a')[0].name;
		iconCls = iconCls.toLowerCase();
		var topMenuId = iconCls.split('_')[1];

		Ext.Ajax.request({
			url : __ctxPath + '/panelTreeMenu.do?topMenuId=' + topMenuId,
			success : function(response, options) {
				var arr = eval(response.responseText);
				var __activedPanelId = getCookie("__activedPanelId");

				// 加菜中间菜单
				if (show) {
					me.showMenu(arr, topMenuId);
				}

				westPanel.removeAll();
				westPanel.doLayout();

				for (var i = 0; i < arr.length; i++) {
					var panel = new Ext.tree.Panel({
								id : arr[i].id,
								title : arr[i].text,
								iconCls : arr[i].iconCls,
								layout : 'fit',
								animate : true,
								border : false,
								autoScroll : true,
								root : {
									expanded : true,
									text : "MyRoot",
									children : arr[i].subXml.Menus.children
								},
								listeners : {
									itemclick : App.clickNode
								},
								rootVisible : false
							});
					westPanel.add(panel);
					panel.on('expand', function(p) {
						// 记住上次点激的panel
						var expires = new Date();
						expires.setDate(expires.getDate() + 30);
						setCookie("__activedPanelId", p.id, expires, __ctxPath);
					});
					// 激活上次点击的panel
					if (arr[i].id == __activedPanelId) {
						westPanel.layout.activeItem = panel;
					}
				}
				westPanel.doLayout();
			}
		});
	}

});
